package com.feng.controller;

import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AddRoleDto;
import com.feng.domain.dto.AdminUpdateRoleStatusDto;
import com.feng.domain.dto.UpdateRoleDto;
import com.feng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, String roleName, String status){
        return roleService.getList(pageNum, pageSize, roleName, status);
    }

    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody AdminUpdateRoleStatusDto statusDto){
        return roleService.updateRoleStatus(statusDto);
    }

    @PostMapping
    public ResponseResult addRole(@RequestBody AddRoleDto addRoleDto){
        return roleService.addRole(addRoleDto);
    }

    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @GetMapping("/{id}")
    public ResponseResult getRole(@PathVariable("id") Long id){
        return roleService.getRole(id);
    }

    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @PutMapping
    public ResponseResult updateRole(@RequestBody UpdateRoleDto updateRoleDto){
        return roleService.updateRole(updateRoleDto);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delRole(@PathVariable("id") List<Long> ids){
        return roleService.delRole(ids);
    }

    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        return roleService.listAllRole();
    }
}
